package org.teststack.token;


import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

    public class ExtentTestNGListener extends TestListenerAdapter {

        private ExtentReports extent;
        private ExtentTest test;

        @Override
        public void onStart(ITestContext testContext) {
            // 初始化ExtentReports报告对象
            extent = new ExtentReports();

            // 配置报告的HTML输出路径
            ExtentSparkReporter spark = new ExtentSparkReporter("test-output/extent-report.html");

            // 将报告对象和报告输出关联
            extent.attachReporter(spark);
        }

        @Override
        public void onTestStart(ITestResult result) {
            // 创建测试
            test = extent.createTest(result.getMethod().getMethodName(), result.getMethod().getDescription());
        }

        @Override
        public void onTestSuccess(ITestResult result) {
            // 记录测试结果
            test.log(Status.PASS, "Test Passed");
        }

        @Override
        public void onTestFailure(ITestResult result) {
            // 记录测试结果
            test.log(Status.FAIL, "Test Failed");
        }

        @Override
        public void onFinish(ITestContext testContext) {
            // 结束测试，并生成报告
            extent.flush();
        }
    }

